template <class T>
ListNode<T>::ListNode():Next(nullptr){}

template <class T>
ListNode<T>::ListNode(const ListNode<T> &pt)
{
	Item = pt.T;
	Next = nullptr;
}

template <class T>
ListNode<T>::~ListNode(){}

template <class T>
UList<T>::UList():head(nullptr){}

template <class T>
UList<T>::UList(const UList<T> &pt)
{
	if(pt.head == nullptr)
	{
		head = nullptr;
	}
	else
	{
		head = new ListNode<T>;
		head->Item = (pt.head)->Item;
		ListNode<T> *now = head;
		for(ListNode<T> *curr = (pt.head)->Next; curr != nullptr ; curr = curr->Next)
		{
			now->Next = new ListNode<T>;
			now = now->Next;
			now->Item = curr->Item;
		}
		now->Next = nullptr;
	}
}

template <class T>
UList<T>::~UList()
{
	ListNode<T> *now = head;
	ListNode<T> *nex = nullptr;
	while(now != nullptr)
	{
		nex = now->Next;
		delete now;
		now = nex;
	}
}

template <class T>
bool UList<T>::insert(const int pos,const T x)
{
	if(pos == 0)
	{
		if(head == nullptr)
		{
			head = new ListNode<T>;
			head->Item = x;
			head->Next = nullptr;
			return true;
		}
		else
		{
			ListNode<T> *curr = new ListNode<T>;
			curr->Item = head->Item;
			curr->Next = head->Next;
			head->Item = x;
			head->Next = curr;
			return true;
		}
	}

	if(pos >= 0 && pos < this->size())
	{
		ListNode<T> *curr;
		int cnt = 0;
		for(curr = head; cnt < pos; curr = curr->Next, cnt++);
		ListNode<T> *nex = new ListNode<T>;
		nex->Item = curr->Item;
		curr->Item = x;
		nex->Next = curr->Next;
		curr->Next = nex;
		return true;
	}
	else
		return false;
}

template <class T>
bool UList<T>::push_back(const T x)
{
	if(this->empty())
	{
		head = new ListNode<T>;
		head->Item = x;
		head->Next = nullptr;
	}
	else
	{
		ListNode<T> *curr;
		for(curr = head; curr->Next != nullptr; curr = curr->Next);

		curr->Next = new ListNode<T>;
		curr = curr->Next;
		curr->Item = x;
		curr->Next = nullptr;
	}

	return true;
}

template <class T>
bool UList<T>::remove(const int pos)
{
	if(pos<0 || pos>=this->size() )return false;

	if(pos == 0)
	{
		ListNode<T> *now = head->Next;
		delete head;
		head = now;
	}
	else
	{
		ListNode<T> *curr;
		int cnt = 0;
		for(curr = head; curr->Next != nullptr && cnt != pos-1; curr = curr->Next, cnt++);


		ListNode<T> *nex = curr->Next;
		curr->Next = nex->Next;
		delete curr;
	}

	return true;
}

template <class T>
bool UList<T>::empty()
{
	if(head != nullptr)
		return false;
	return true;
}

template <class T>
int UList<T>::size()
{
	if(head == nullptr)
		return 0;

	int cnt=1;
	for(auto curr = head; curr->Next != nullptr; curr = curr->Next, cnt++);
	return cnt;
}

template <class T>
bool UList<T>::clear()
{
	ListNode<T> *now = head, *nex = nullptr;
	while(now != nullptr)
	{
		nex = now->Next;
		delete now;
		now = nex;
	}
	head = nullptr;
	return true;
}

template <class T>
T& UList<T>::operator[](const int pos)
{
	if(pos<0 || pos>=this->size() )return head->Item;

	int cnt = 0;
	ListNode<T> *now = head;
	while(cnt != pos)
	{
		now = now->Next;
		cnt++;
	}

	return now->Item;
}
